package com.mutual.appUserInfo.service.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.druid.support.logging.Log;
import com.mutual.appUserInfo.dao.AppUserInfoProductDao;
import com.mutual.appUserInfo.pojo.AppUserInfo;
import com.mutual.appUserInfo.pojo.AppUserInfoProduct;
import com.mutual.appUserInfo.service.AppUserInfoProductService;
import com.mutual.appUserInfo.service.AppUserInfoService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.commom.util.DateUtil;
import com.mutual.commom.util.SystemUtil;
import com.mutual.family.pojo.Family;
import com.mutual.family.service.FamilyService;
import com.mutual.product.pojo.Product;
import com.mutual.product.service.ProductService;
import com.mutual.vipAnnualFee.pojo.VipAnnualFee;
import com.mutual.vipAnnualFee.service.VipAnnualFeeService;

@Service
public class AppUserInfoProductServiceImpl implements AppUserInfoProductService {

	@Resource
	private AppUserInfoProductDao appUserInfoProductDao;
	
	@Resource
	private ProductService productService;
	
	@Resource
	private AppUserInfoService appUserInfoService;
	
	@Resource
	private VipAnnualFeeService vipAnnualFeeService;
	
	@Resource
	private FamilyService familyService;
	
	@Transactional
	@Override
	public void insert(AppUserInfoProduct entity) {
		Product product = productService.selectByPrimaryKey(entity.getProductId());
		if(product.getApplyUserType()==Product.APPLYUSERTYPE_VIP) {//如果产品是vip产品，则更新app用户为vip
			AppUserInfo appUserInfo =new AppUserInfo();
			appUserInfo.setId(entity.getAppUserId());
			appUserInfo.setAppUserType(AppUserInfo.VIPSTATE_VIP);
			appUserInfoService.updateByPrimaryKey(appUserInfo);
		}
		VipAnnualFee vipAnnualFee =new VipAnnualFee();
		vipAnnualFee.setIdCode(entity.getIdCode());
		BigDecimal bigDecimal = vipAnnualFeeService.getVipAnnuaFee(vipAnnualFee);
		if(bigDecimal.compareTo(BigDecimal.ZERO)>0) {
			vipAnnualFee.setAnnualFee(bigDecimal);
			vipAnnualFee.setAppUserId(entity.getAppUserId());
			vipAnnualFee.setCreateDate(new Date());
			vipAnnualFee.setIdCode(entity.getIdCode());
			vipAnnualFee.setPayYear(DateUtil.getYear());
			vipAnnualFeeService.insert(vipAnnualFee);
		}
		appUserInfoProductDao.insertSelective(entity);
	}

	@Override
	@Transactional
	public void updateByPrimaryKey(AppUserInfoProduct entity) {
		appUserInfoProductDao.updateByPrimaryKeySelective(entity);
	}

	@Override
	public AppUserInfoProduct selectByPrimaryKey(Serializable entityid) {
		return appUserInfoProductDao.selectByPrimaryKey(entityid);
	}

	@Override
	public List<AppUserInfoProduct> selectAppUserInfoProductList(AppUserInfoProduct appUserInfoProduct) {
		return appUserInfoProductDao.selectObjectList(appUserInfoProduct);
	}

	@Override
	public int selectTotalCount(Map<String, Object> param) {
		return appUserInfoProductDao.selectTotalCount(param);
	}

	@Override
	public List<AppUserInfoProduct> selectAppUserInfoProductList(Map<String, Object> param) {
		if(param.containsKey("page")){
			int pageNo=SystemUtil.getPageNo(param.get("page"));
			int pageSize=Integer.parseInt(param.get("limit").toString());
			param.put("startNo", (pageNo-1)*pageSize);
			param.put("pageSize", pageSize);
		}
		return appUserInfoProductDao.selectAppUserInfoProducts(param);
	}
	
	public static void main(String[] args) {
		System.out.println(BigDecimal.valueOf(1).compareTo(BigDecimal.ZERO));
	}

	@Override
	@Transactional
	public ReturnResult buyProduct(AppUserInfoProduct appUserInfoProduct) {
		List<AppUserInfoProduct> selectDataList = appUserInfoProductDao.selectObjectList(appUserInfoProduct);
		if(!selectDataList.isEmpty()){
			ReturnResult returnResult=new ReturnResult();
			if(selectDataList.get(0).getStatus()==AppUserInfoProduct.STATUS_BUY_SUCCESS){
				returnResult.setStatus(ReturnResult.STATUS_FAILED);
				returnResult.setReturnMsg("已经支付成功，无需再次购买该产品");
				return returnResult;
			}else if(selectDataList.get(0).getStatus()==AppUserInfoProduct.STATUS_BUY_PEND_PAYMENT){
				returnResult.setReturnMsg("待付款，可直接付款");
			}else{
				returnResult.setReturnMsg("付款失败，可直接付款");
			}
			appUserInfoProduct.setId(selectDataList.get(0).getId());
			appUserInfoProductDao.updateByPrimaryKeySelective(appUserInfoProduct);
			returnResult.setStatus(ReturnResult.STATUS_SUCCESS);
			VipAnnualFee vipAnnualFee = new VipAnnualFee();
			vipAnnualFee.setIdCode(appUserInfoProduct.getIdCode());
			BigDecimal allAmount = selectDataList.get(0).getAmount();
			BigDecimal bigDecimal = vipAnnualFeeService.getVipAnnuaFee(vipAnnualFee);//年费是否支付
			if (bigDecimal.compareTo(BigDecimal.ZERO) > 0) {//如果没有支付
				allAmount=allAmount.add(bigDecimal);//把总共支付的金额累计
			}
			selectDataList.get(0).setAmount(allAmount);
			returnResult.setData(selectDataList.get(0));
			return returnResult;
		}
		Product product=productService.selectByPrimaryKey(appUserInfoProduct.getProductId());
		appUserInfoProduct.setProductName(product.getName());
		appUserInfoProduct.setAmount(product.getApplyAmount());
		String applyPeopleName = null;
		if(appUserInfoProduct.getType()==AppUserInfoProduct.TYPE_FAMILY){
			Family family = familyService.selectByPrimaryKey(appUserInfoProduct.getApplyPeopleId());
			applyPeopleName=family.getRealName();
		}else{
			AppUserInfo appUserInfo=appUserInfoService.selectByPrimaryKey(appUserInfoProduct.getApplyPeopleId());
			applyPeopleName = appUserInfo.getRealName();
		}
		appUserInfoProduct.setApplyPeopleName(applyPeopleName);
		return this.saveProduct(product, appUserInfoProduct);
	}

	/**
	 * 保存用户产品数据
	 * @param product
	 * @param entity
	 * @return
	 */
	private ReturnResult saveProduct(Product product, AppUserInfoProduct entity) {
		ReturnResult result=new ReturnResult();
		entity.setApplyDate(new Date());
		VipAnnualFee vipAnnualFee = new VipAnnualFee();
		vipAnnualFee.setIdCode(entity.getIdCode());
		BigDecimal allAmount = entity.getAmount();
		BigDecimal bigDecimal = vipAnnualFeeService.getVipAnnuaFee(vipAnnualFee);//年费是否支付
		if (bigDecimal.compareTo(BigDecimal.ZERO) > 0) {//如果没有支付
			vipAnnualFee.setAnnualFee(bigDecimal);
			vipAnnualFee.setAppUserId(entity.getAppUserId());
			vipAnnualFee.setCreateDate(new Date());
			vipAnnualFee.setIdCode(entity.getIdCode());
			vipAnnualFee.setPayYear(DateUtil.getYear());
			vipAnnualFeeService.insert(vipAnnualFee);
			allAmount=allAmount.add(bigDecimal);//把总共支付的金额累计
		}
		appUserInfoProductDao.insertSelective(entity);
		entity.setAmount(allAmount);
		result.setData(entity);
		result.setStatus(ReturnResult.STATUS_SUCCESS);
		return result;
	}

	@Override
	@Transactional
	public int updateBuyProductStatus(AppUserInfoProduct entity) {
		if(StringUtils.isEmpty(entity.getIdCode())){
			AppUserInfoProduct selectData=appUserInfoProductDao.selectObjectList(entity).get(0);
			VipAnnualFee vipAnnualFee =new VipAnnualFee();
			vipAnnualFee.setIdCode(selectData.getIdCode());
			vipAnnualFee.setPayYear(DateUtil.getYear());
			vipAnnualFee.setCreateDate(new Date());
			vipAnnualFee.setStatus(entity.getStatus());
			vipAnnualFeeService.updatePaymentStatus(vipAnnualFee);
			AppUserInfo appUserInfo = new AppUserInfo();
			appUserInfo.setId(selectData.getAppUserId());
			if(entity.getStatus()==AppUserInfoProduct.STATUS_BUY_SUCCESS){
				appUserInfo.setAppUserType(AppUserInfo.VIPSTATE_VIP);
			}
			appUserInfoService.updateByPrimaryKey(appUserInfo);
		}
		appUserInfoProductDao.updateByPrimaryKeySelective(entity);
		return 1;
	}

	@Override
	public List<AppUserInfoProduct> selectAllObjPage(Map<String, Object> param) {
		if(param.containsKey("page")){
			int pageNo=SystemUtil.getPageNo(param.get("page"));
			int pageSize=Integer.parseInt(param.get("limit").toString());
			param.put("startNo", (pageNo-1)*pageSize);
			param.put("pageSize", pageSize);
		}
		return appUserInfoProductDao.selectAllObjPage(param);
	}

	@Override
	public ReturnResult sendNoticeSMS(Map<String, Object> param) {
		ReturnResult result=new ReturnResult();
		return null;
	}
}
